package de.grosjohann.android.feuerwehr.service;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import de.grosjohann.android.feuerwehr.IUpdateUIListener;
import de.grosjohann.android.feuerwehr.webservice.FMSWsFacade;

public class FMSAuthService
  extends IntentService {

  public static final String FMS_AUTHORIZATION = "feuerwehr.FMSStatusAuthorization";

  public static IUpdateUIListener updateUIListener;

  private static final String NAME = "FMSAuthService";

  public FMSAuthService() {
    super(NAME);
  }

  @Override
  public void onCreate() {
    super.onCreate();

    Log.d(NAME, "create");
  }

  @Override
  protected void onHandleIntent(Intent newIntent) {
    Log.d(NAME, "handle");

    Context context = getApplicationContext();
    Bundle bundle = newIntent.getExtras();
    if (bundle != null) {
      try {
        String json = FMSWsFacade.getFMSAuthorization(context, bundle.getString(FMSAuthService.FMS_AUTHORIZATION));

        if (updateUIListener != null) {
          updateUIListener.updateUI(json);
        }
      } catch (Exception ex) {
        Log.e(NAME, ex.getMessage(), ex);
      }
    }
  }
}
